package com.frx.fakeroutexposed.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import de.robv.android.xposed.XSharedPreferences;

/** 工具类,用于参数配置
 *	<br>
 * 同时也用于模拟经纬度的时候,存放数据
 * @author viki
 *
 */
@SuppressLint("WorldReadableFiles")
public class SettingsHelper {

    /**
     * 本程序自用的<Br>SharedPreferences
     */
    private SharedPreferences mPreferences = null;
    /**
     * 供其它程序使用的Preferences<Br>XSharedPreferences
     */
    private XSharedPreferences mXposedPreferences = null;
    /**
     * 供其它程序使用的Preferences<br>
     * 并且是只读属性
     */
    public SettingsHelper() {
        mXposedPreferences = new XSharedPreferences("com.frx.fakeroutexposed",MY_NAMES.NAME_Preferences_gps_data);
        mXposedPreferences.makeWorldReadable();

        this.reload();
    }
    /**
     * 本程序自用的 SharedPreferences
     */
    @SuppressWarnings("deprecation")
    public SettingsHelper(Context context) {
        this.mPreferences = context.getSharedPreferences(MY_NAMES.NAME_Preferences_gps_data, Context.MODE_WORLD_READABLE);
    }

    public String getString(String key, String defaultValue) {
        if (mPreferences != null) {
            return mPreferences.getString(key, defaultValue);
        } else if (mXposedPreferences != null) {
            return mXposedPreferences.getString(key, defaultValue);
        }

        return defaultValue;
    }

    public int getInt(String key, int defaultValue) {
        if (mPreferences != null) {
            return mPreferences.getInt(key, defaultValue);
        } else if (mXposedPreferences != null) {
            return mXposedPreferences.getInt(key, defaultValue);
        }

        return defaultValue;
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        if (mPreferences != null) {
            return mPreferences.getBoolean(key, defaultValue);
        } else if (mXposedPreferences != null) {
            return mXposedPreferences.getBoolean(key, defaultValue);
        }

        return defaultValue;
    }

    public void setString(String key, String value) {
        Editor editor = null;
        if (mPreferences != null) {
            editor = mPreferences.edit();
        } else if (mXposedPreferences != null) {
            editor = mXposedPreferences.edit();
        }

        if (editor != null) {
            editor.putString(key, value);
            editor.commit();
        }
    }

    public void setBoolean(String key, boolean value) {
        Editor editor = null;
        if (mPreferences != null) {
            editor = mPreferences.edit();
        } else if (mXposedPreferences != null) {
            editor = mXposedPreferences.edit();
        }

        if (editor != null) {
            editor.putBoolean(key, value);
            editor.commit();
        }
    }

    public void setInt(String key, int value) {
        Editor editor = null;
        if (mPreferences != null) {
            editor = mPreferences.edit();
        } else if (mXposedPreferences != null) {
            editor = mXposedPreferences.edit();
        }

        if (editor != null) {
            editor.putInt(key, value);
            editor.commit();
        }
    }

    /**
     * Reload the settings from file if they have changed. 
     */
    public void reload() {
        if (mXposedPreferences != null) {
            mXposedPreferences.reload();
        }
    }
}
